<?php

declare(strict_types=1);

namespace app\shop\subscribe;

use think\Event;
use sheep\facade\Auth;
use sheep\exception\SheepException;
use sheep\model\auth\Admin;
use app\shop\model\goods\Goods as GoodsModel;
use yunwuxin\facade\Notification;

class Goods
{
    public function onShopGoodsStockWarning($params)
    {
        $goodsSkuPrice = $params['goodsSkuPrice'];
        $stock_warning = $params['stock_warning'];

        $goods = GoodsModel::find($goodsSkuPrice['goods_id']);

        // 商品库存不足，请及时补货
        $admins = Admin::select()->filter(function ($admin) {
            return $admin->hasAccess($admin, [      // 商品列表补货 和 库存预警补货
                'shop.admin.goods.goods.addstock',
                'shop.admin.goods.stockwarning.addstock',
            ]);
        });
        if (!$admins->isEmpty()) {
            Notification::send(
                $admins,
                new \app\shop\notification\goods\StockWarning([
                    'goods' => $goods,
                    'goodsSkuPrice' => $goodsSkuPrice,
                    'stock_warning' => $stock_warning
                ])
            );
        }
    }
}
